/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    bool hasPathSum(TreeNode *root, int sum) {
        if (root == nullptr)
            return false;

        queue<TreeNode*> q_node;
        queue<int> q_val;
        q_node.push(root);
        q_val.push(root->val);
        while (!q_node.empty())
        {
            TreeNode *now = q_node.front();
            int tmp = q_val.front();
            q_node.pop();
            q_val.pop();
            if (now->left == nullptr && now->right == nullptr)
            {
                if (tmp == sum)
                    return true;
                continue;
            }
            if (now->left != nullptr)
            {
                q_node.push(now->left);
                q_val.push(now->left->val + tmp);
            }
            if (now->right != nullptr)
            {
                q_node.push(now->right);
                q_val.push(now->right->val + tmp);
            }
        }
        return false;
    }
};